package cn.dgiot.fd.controller;

import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.iot.model.v20180120.CreateOTAFirmwareResponse;
import com.aliyuncs.iot.model.v20180120.CreateOTAModuleResponse;
import com.aliyuncs.iot.model.v20180120.CreateOTAStaticUpgradeJobResponse;
import com.aliyuncs.iot.model.v20180120.DeleteOTAFirmwareResponse;
import com.aliyuncs.iot.model.v20180120.DeleteOTAModuleResponse;
import com.aliyuncs.iot.model.v20180120.ListOTAModuleByProductResponse;
import com.aliyuncs.iot.model.v20180120.UpdateOTAModuleResponse;

import cn.dgiot.fd.model.OTAModel;
import cn.dgiot.fd.service.IOTAService;

/**
 * [设备升级]
 * @author
 *
 */
@RestController
@RequestMapping("/ota")
public class OTAController {

	private Logger logger = LoggerFactory.getLogger(OTAController.class);
	
	@Autowired
	private IOTAService otaService;
	
	/**
	 * 
	 * @param deviceModel
	 * @param response
	 * @return
	 * @throws FileNotFoundException 
	 */
	@RequestMapping("/createOTAFirmware")
	public String createOTAFirmware(@RequestBody OTAModel otaModel,HttpServletResponse response) throws Exception {
		// 调用该接口生成升级包文件上传到OSS的URL及详细信息
		CreateOTAFirmwareResponse createOTAFirmwareResponse = otaService.createOTAFirmware(otaModel);
		String result = JSON.toJSONString(createOTAFirmwareResponse);
		return result;
	}
	
	/**
	 * 
	 * @param deviceModel
	 * @param response
	 * @return
	 * @throws FileNotFoundException 
	 */
	@RequestMapping("/deviceUpgrade")
	public String deviceUpgrade(@RequestBody OTAModel otaModel,HttpServletResponse response) throws Exception {
		// 调用该接口生成升级包文件上传到OSS的URL及详细信息
		CreateOTAStaticUpgradeJobResponse createOTAStaticUpgradeJobResponse= otaService.createOTAStaticUpgradeJob(otaModel);
		String result = JSON.toJSONString(createOTAStaticUpgradeJobResponse);
		return result;
	}
	
	/**
	 * [删除升级包]
	 * @param deviceModel
	 * @param response
	 * @return
	 * @throws FileNotFoundException 
	 */
	@RequestMapping("/deleteOTAFirmware")
	public String deleteOTAFirmware(@RequestBody OTAModel otaModel,HttpServletResponse response) throws Exception {
		// 调用该接口生成升级包文件上传到OSS的URL及详细信息
		DeleteOTAFirmwareResponse deleteOTAFirmwareResponse= otaService.deleteOTAFirmware(otaModel);
		String result = JSON.toJSONString(deleteOTAFirmwareResponse);
		return result;
	}
	
	
	
	/**
	 * [创建升级模块]
	 * @param otaModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/createOTAModule")
	public String createOTAModule(@RequestBody OTAModel otaModel,HttpServletResponse response) throws Exception {
		CreateOTAModuleResponse deleteOTAFirmwareResponse= otaService.createOTAModule(otaModel);
		String result = JSON.toJSONString(deleteOTAFirmwareResponse);
		return result;
	}
	
	
	/**
	 * [修改升级模块]
	 * @param otaModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/updateOTAModule")
	public String updateOTAModule(@RequestBody OTAModel otaModel,HttpServletResponse response) throws Exception {
		UpdateOTAModuleResponse deleteOTAFirmwareResponse= otaService.updateOTAModule(otaModel);
		String result = JSON.toJSONString(deleteOTAFirmwareResponse);
		return result;
	}
	
	

	/**[删除升级模块]
	 * @param otaModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/deleteOTAModule")
	public String deleteOTAModule(@RequestBody OTAModel otaModel,HttpServletResponse response) throws Exception {
		DeleteOTAModuleResponse deleteOTAFirmwareResponse= otaService.deleteOTAModule(otaModel);
		String result = JSON.toJSONString(deleteOTAFirmwareResponse);
		return result;
	}
	
	/**[查询创建的升级模块]
	 * @param otaModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/listOTAModuleByProduct")
	public String listOTAModuleByProduct(@RequestBody OTAModel otaModel,HttpServletResponse response) throws Exception {
		ListOTAModuleByProductResponse listOTAModuleByProductResponse= otaService.listOTAModuleByProduct(otaModel);
		String result = JSON.toJSONString(listOTAModuleByProductResponse);
		return result;
	}


}
